package com.macro.mall.tiny.modules.foster.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.macro.mall.tiny.common.api.CommonPage;
import com.macro.mall.tiny.common.api.CommonResult;
import com.macro.mall.tiny.modules.foster.model.Item;
import com.macro.mall.tiny.modules.foster.service.ItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import static com.baomidou.mybatisplus.core.toolkit.IdWorker.getId;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhuyi
 * @since 2024-04-03
 */
@Api(tags = "ItemController", description = "商品管理")
@RestController
@RequestMapping("/foster/item")
@CrossOrigin
public class ItemController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ItemController.class);

    @Autowired
    private ItemService itemService;
    /**
     * 添加商品
     *
     * @param item
     * @return
     */
    @ApiOperation(value = "添加商品")
    @RequestMapping(value = "/addItem", method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('admin')")
    public CommonResult addItem(@RequestBody Item item) {
        item.setId(getId());
        itemService.save(item);
        LOGGER.info("添加商品信息成功");
        return CommonResult.success("添加商品信息成功");
    }

    /**
     * 删除商品
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除商品")
    @RequestMapping(value = "/deleteItem/{id}", method = RequestMethod.DELETE)
    @PreAuthorize("hasAuthority('admin')")
    public CommonResult deleteItem(@PathVariable Long id) {
        itemService.removeById(id);
        LOGGER.info("删除商品信息成功");
        return CommonResult.success("删除商品信息成功");
    }

    /**
     * 修改商品
     *
     * @param item
     * @return
     */
    @ApiOperation(value = "修改商品")
    @RequestMapping(value = "/updateItem", method = RequestMethod.PUT)
    @PreAuthorize("hasAuthority('admin')")
    public CommonResult updateItem(@RequestBody Item item) {
        itemService.updateById(item);
        LOGGER.info("修改商品信息成功");
        return CommonResult.success("修改商品信息成功");
    }

    /**
     * 分页查询商品列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation("分页查询商品列表")
    @PreAuthorize("hasAnyAuthority('user','admin')")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<Item>> list(@RequestParam(value = "pageNum", defaultValue = "1")
                                                       @ApiParam("页码") Integer pageNum,
                                                       @RequestParam(value = "pageSize", defaultValue = "5")
                                                       @ApiParam("每页数量") Integer pageSize,
                                                       @RequestParam(value = "type",required = false)
                                                       @ApiParam("商品类型,可不传获取全部") Integer type){

        Page<Item> page = new Page<>(pageNum, pageSize);
        Page<Item> pageResult = itemService.pageItem(page, type);
        LOGGER.info("商品列表查询状态：查询成功");
        return CommonResult.success(CommonPage.restPage(pageResult));
    }

    /**
     * 获取指定id的商品
     * @param id
     * @return
     */
    @ApiOperation("获取指定id的商品")
    @RequestMapping(value = "/getItemById/{id}", method = RequestMethod.GET)
    @PreAuthorize("hasAnyAuthority('user','admin')")
    public CommonResult<Item> getItemById(@PathVariable("id") Long id) {
        Item item = itemService.getById(id);
        LOGGER.info("指定id的商品查询状态：查询成功");
        return CommonResult.success(item);
    }

}


